# 准备工作

import requests,json
import pandas as pd
key = 'ac1c16d9ba930c1f889b26fb9a392c2f'


# 地理编码
def geo(key,address):  #记得打冒号！！！
    url_geo = 'https://restapi.amap.com/v3/geocode/geo?parameters'
    params_geo = {
        "key":key,
        "address":address,
        "output":"json"
    }
    r = requests.get(url= url_geo,params=params_geo)
    results = r.json()
    return results

# 逆地理编码
def regeo(key,location,extensions="base",radius=None,poitype=None):
    """"""
    url = "https://restapi.amap.com/v3/geocode/regeo?parameters"
    params = {
        "key":key,
        "location":location,
        "extensions":extensions,
        "radius":radius,
        "poitype":poitype
    }
    r = requests.get(url,params)
    results = r.json()
    return results

# 地理编码location
def geocode(key,address,city=None,batch=None):
    """  """
    url = "https://restapi.amap.com/v3/geocode/geo?parameters"
    params = {
        "key":key,
        "address":address,
        "city":city,
        "batch":batch
    }
    response = requests.get(url,params=params)
    results = response.json()['geocodes'][0]['location']
    return results

# 步行
def walk(key,origin,destination):#不要忘记打冒号
    walk_url ='https://restapi.amap.com/v3/direction/walking?parameters'
    params = {
        "key":key,
        "origin":origin,
        "destination":destination
    }
    response = requests.get(walk_url,params=params)
    results = response.json()
    return results

# 公交车
def bus(key,origin,destination,city):
    bus_url = "https://restapi.amap.com/v3/direction/transit/integrated?parameters"
    params ={
        "key":key,
        "origin":origin,
        "destination":destination,
        "city":city
    }
    r = requests.get(bus_url,params=params)
    results = r.json()
    return results

# 驾车
def car(key,origin,destination):
    car_url = 'https://restapi.amap.com/v3/direction/driving?parameters'
    params ={
        "key":key,
        "origin":origin,
        "destination":destination,
        "stategy":"0"
    }
    r = requests.get(car_url,params=params)
    results = r.json()
    return results

# 行政区域查询
def area(key,keywords,subdistrict,page,extensions):
    area_url='https://restapi.amap.com/v3/config/district?parameters'
    params={
        "key":key,#记得填写！不然会返回INVALID_USERKEY
        "keywords":keywords,
        "page":page,
        "extensions":extensions,
        "subdistrict":subdistrict
    }
    r = requests.get(area_url,params=params)
    results = r.json()
    return results

# 搜索POI
def search(key,keywords,city,citylimit,children,offset,page,extensions,callback=None,output="JSON"):
    """关键词搜索"""
    search_url = "https://restapi.amap.com/v3/place/text?parameters"
    params = {
    "key":key,
    "keywords":keywords,
    "city":city,
    "citylimit":citylimit,
    "children":children,
    "offset":offset,
    "page":page,
    "extensions":extensions,
    "output":"json"   
    }
    response = requests.get(search_url,params=params)
    results = response.json()
    return results

# IP定位
def ip_position(key,output="JSON",ip=None,sig=None):
    ip_url = 'https://restapi.amap.com/v3/ip?parameters'
    params ={
        "key":key,
        "output":"JSON",
        "ip":ip
    }
    #get方法返回值
    response = requests.get(ip_url,params=params)
    #json格式返回
    results = response.json()
    #返回值
    return results

# 静态地图
def map(key,location,zoom,size,scale,traffic,markers=None,labels=None,paths=None,sig=None):
    #获取静态地图信息
    url='https://restapi.amap.com/v3/staticmap?parameters'
    params={
    'key':key,
    'location':location,
    'zoom':zoom,
    'scale':scale,
    'markers':markers,
    'labels':labels,
    'paths':paths,
    'traffic':traffic,
    'sig':sig
    }
    r=requests.get(url,params)
    data=r.url
    return data

# 坐标转换
def coordinate(key,locations,coordsys,output='JSON'):
    trans_url ='https://restapi.amap.com/v3/assistant/coordinate/convert?parameters'
    params={
        "key":key,
        "locations":locations,
        "coordsys":coordsys,
        "output":"JSON"
    }
    response = requests.get(trans_url,params=params)
    results = response.json()
    return results

# 天气查询
def weather(key,city,extensions,output):
    weather_url = 'https://restapi.amap.com/v3/weather/weatherInfo?parameters'
    params ={
        "key":key,
        "city":city,
        "extensions":extensions,
        "output":"JSON"
    }
    response = requests.get(weather_url,params)
    results = response.json()
    return results

# 输入提示
def input(key,keywords,location,citylimit,datatype,type=None):
    input_url = 'https://restapi.amap.com/v3/assistant/inputtips?parameters'
    params ={
        "key":key,
        "keywords":keywords,
        "location":location,
        "citylimit":citylimit,
        "datatype":datatype,
        
    }
    response = requests.get(input_url,params=params)
    results = response.json()
    return results